package com.zirenx.sys.service;

import com.jfinal.plugin.activerecord.Db;
import com.zirenx.sys.model.SysRoleMenu;

import java.util.ArrayList;
import java.util.List;

public class SysRoleMenuService {
    SysRoleMenu dao = new SysRoleMenu().dao();

    /**
     * 获取菜单id
     * @param roleId 角色
     * @return
     */
    public List<Long> getListMenuIdByRoleId(String roleId){
        return Db.query("select DISTINCT menu_id from "+dao.getTableName() +" t where t.role_id = ?",roleId);
    }

    public void batchSave(String roleId,String[] menuIds){
        List<SysRoleMenu> list = new ArrayList<>();
        SysRoleMenu bean = null;
        for (String id : menuIds){
            bean = new SysRoleMenu();
            bean.setMenuId(Long.valueOf(id));
            bean.setRoleId(Long.valueOf(roleId));
            list.add(bean);
        }
        Db.batchSave(list, list.size());
    }

    public void batchRemove(String roleId){
        Db.delete(" delete from "+dao.getTableName() +" where role_id = ?",roleId);
    }
}
